package com.ruoyi.system.service.award.teach.impl;

import java.util.List;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.award.teach.TeachAwardInfo;
import com.ruoyi.system.domain.award.teach.TeachAwardTeacher;
import com.ruoyi.system.mapper.award.teach.TeachAwardInfoMapper;
import com.ruoyi.system.mapper.award.teach.TeachAwardTeacherMapper;
import com.ruoyi.system.service.award.teach.ITeachAwardInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;

import static com.ruoyi.common.utils.SecurityUtils.getUserId;

/**
 * 教学获奖信息Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-28
 */
@Service
public class TeachAwardInfoServiceImpl implements ITeachAwardInfoService
{
    @Autowired
    private TeachAwardInfoMapper teachAwardInfoMapper;
    @Autowired
    private TeachAwardTeacherMapper teachAwardTeacherMapper;
    /**
     * 查询教学获奖信息
     * 
     * @param id 教学获奖信息主键
     * @return 教学获奖信息
     */
    @Override
    public TeachAwardInfo selectTeachAwardInfoById(Long id)
    {
        TeachAwardInfo rc = teachAwardInfoMapper.selectTeachAwardInfoById(id);
        TeachAwardTeacher ra = new TeachAwardTeacher();
        ra.setTeachId(id);
        List<TeachAwardTeacher> teachers = teachAwardTeacherMapper.selectTeachAwardTeacherList(ra);
        rc.setTeachers(teachers);
        return rc;
    }

    /**
     * 查询教学获奖信息列表
     * 
     * @param teachAwardInfo 教学获奖信息
     * @return 教学获奖信息
     */
    @Override
    public List<TeachAwardInfo> selectTeachAwardInfoList(TeachAwardInfo teachAwardInfo)
    {
//        //如果是老师，则只显示自己上传的
//        if(teachAwardInfo.getCurrentRole()!=null&&teachAwardInfo.getCurrentRole().equals("teacher")){
//            teachAwardInfo.setTeacherId(getUserId());
//        }
//        //如果是部门，则显示老师是系里的所有
//        if(teachAwardInfo.getCurrentRole()!=null&&teachAwardInfo.getCurrentRole().equals("department")){
//            teachAwardInfo.setDeptId(SecurityUtils.getDeptId());
//        }
        List<TeachAwardInfo> rc = teachAwardInfoMapper.selectTeachAwardInfoList(teachAwardInfo);
        for(TeachAwardInfo rc1 : rc){
            TeachAwardTeacher ra = new TeachAwardTeacher();
            ra.setTeachId(rc1.getId());
            List<TeachAwardTeacher> teachers = teachAwardTeacherMapper.selectTeachAwardTeacherList(ra);
            rc1.setTeachers(teachers);
        }
        return rc;
    }

    /**
     * 新增教学获奖信息
     * 
     * @param teachAwardInfo 教学获奖信息
     * @return 结果
     */
    @Override
    public int insertTeachAwardInfo(TeachAwardInfo teachAwardInfo)
    {
        if(teachAwardInfo.getId()!=null){
            TeachAwardTeacher teacher = new TeachAwardTeacher();
            teacher.setTeachId(teachAwardInfo.getId());
            teacher.setUserId(String.valueOf(teachAwardInfo.getTeacherId()));
            teacher.setUserName(teachAwardInfo.getTeacherName());
            teacher.setRank(teachAwardInfo.getTeacherRank());
            teachAwardTeacherMapper.insertTeachAwardTeacher(teacher);
        }else   {
            int result = teachAwardInfoMapper.insertTeachAwardInfo(teachAwardInfo);
            if(result>0){
                // 插入成功，返回主键ID
                TeachAwardTeacher teacher = new TeachAwardTeacher();
                teacher.setTeachId(teachAwardInfo.getId());
                teacher.setUserId(String.valueOf(teachAwardInfo.getTeacherId()));
                teacher.setUserName(teachAwardInfo.getTeacherName());
                teacher.setRank(teachAwardInfo.getTeacherRank());
                teachAwardTeacherMapper.insertTeachAwardTeacher(teacher);
            }
        }
        return 1;
    }

    /**
     * 修改教学获奖信息
     * 
     * @param teachAwardInfo 教学获奖信息
     * @return 结果
     */
    @Override
    public int updateTeachAwardInfo(TeachAwardInfo teachAwardInfo)
    {
        return teachAwardInfoMapper.updateTeachAwardInfo(teachAwardInfo);
    }

    /**
     * 批量删除教学获奖信息
     * 
     * @param ids 需要删除的教学获奖信息主键
     * @return 结果
     */
    @Override
    public int deleteTeachAwardInfoByIds(Long[] ids)
    {
        for(Long id : ids){
            TeachAwardTeacher ra = new TeachAwardTeacher();
            ra.setTeachId(id);
            List<TeachAwardTeacher> teachers = teachAwardTeacherMapper.selectTeachAwardTeacherList(ra);
            if(!teachers.isEmpty()){
                for (TeachAwardTeacher teacher : teachers) {
                    teachAwardTeacherMapper.deleteTeachAwardTeacherById(teacher.getId());
                }
            }
        }
        return teachAwardInfoMapper.deleteTeachAwardInfoByIds(ids);
    }

    /**
     * 删除教学获奖信息信息
     * 
     * @param id 教学获奖信息主键
     * @return 结果
     */
    @Override
    public int deleteTeachAwardInfoById(Long id)
    {
        return teachAwardInfoMapper.deleteTeachAwardInfoById(id);
    }

    @Override
    public TeachAwardInfo getAwardInfoByName(String data) {
        return teachAwardInfoMapper.getAwardInfoByName( data);
    }
}
